package cn.yuanqiao.web.controller.archive.danganzhengli;

import cn.yuanqiao.archive.appManager.service.ITenantSysConfigService;
import cn.yuanqiao.archive.danganzhengli.domain.ImportArchiveLog;
import cn.yuanqiao.archive.danganzhengli.service.IArchiveDBFService;
import cn.yuanqiao.archive.danganzhengli.service.IImportArchiveLogService;
import cn.yuanqiao.archive.wenjianzhengli.utils.UploadDAO;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;

@RestController
@RequestMapping("/archive/dbfRead")
public class ArchiveDBFController extends BaseController {
    @Autowired
    private IArchiveDBFService archiveDBFService;
    @Autowired
    private ITenantSysConfigService sysConfigService;
    @Autowired
    private IImportArchiveLogService importArchiveLogService;

    @Log(title = "获取表头", businessType = BusinessType.IMPORT)
    @PostMapping("/getTitle")
    public AjaxResult getTitle(@RequestParam("categoryId") Long categoryId, @RequestParam("type") Integer type, MultipartFile file) {
        try {
            String path = sysConfigService.selectConfigByKey("dbfPath");
            File file1 = new File(path);
            if(!file1.exists()){
                file1.mkdirs();
            }
            File dbfFile = new File(path+System.currentTimeMillis()+".dbf");
            file.transferTo(dbfFile);
            JSONObject object = archiveDBFService.readDBFTitle(dbfFile,type,categoryId);
            return AjaxResult.success(object);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return AjaxResult.error();
    }

    @Log(title = "档案导入", businessType = BusinessType.IMPORT)
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("dataStr") String dataStr, MultipartFile file) {
        logger.info(dataStr);
        try {
            //保存文件
            String path = sysConfigService.selectConfigByKey("dbfPath");
            File file1 = new File(path);
            if(!file1.exists()){
                file1.mkdirs();
            }
            File dbfFile = new File(path+System.currentTimeMillis()+".dbf");
            file.transferTo(dbfFile);
            //导入记录
            ImportArchiveLog importArchiveLog = new ImportArchiveLog();
            importArchiveLog.setCreateTime(new Date());
            importArchiveLog.setFileName(dbfFile.getName());
            importArchiveLog.setFilePath(dbfFile.getAbsolutePath());
            importArchiveLog.setSourceType(1);
            //解析入库
            UploadDAO dao = archiveDBFService.saveDBFFile(JSONObject.parseObject(dataStr),dbfFile);
            logger.info(dao.toString());

            importArchiveLog.setCategoryId(dao.getCategory().getId());
            importArchiveLog.setType(dao.getType());
            if (StringUtils.isNotEmpty(dao.getMsg())) {
                importArchiveLog.setResult("失败");
                importArchiveLog.setMsg(dao.getMsg());
                importArchiveLogService.insertImportArchiveLog(importArchiveLog);
                return AjaxResult.error(dao.getMsg());
            } else {
                importArchiveLog.setResult("成功"+dao.getZongshu() + "条");
                importArchiveLog.setMsg("耗时:"+DateUtils.longTileString(dao.getTime()));
                importArchiveLogService.insertImportArchiveLog(importArchiveLog);
                return AjaxResult.success("总计导入" + dao.getZongshu() + "条，耗时:" + DateUtils.longTileString(dao.getTime()));
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return AjaxResult.error("导入失败：" + e.getMessage());
        }
    }
}
